package com.reciver.controller;

import com.reciver.bo.JcBo;
import com.reciver.bo.ZlBo;
import com.reciver.bo.ZlBoSbwy;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import com.reciver.common.annotation.SysLog;
import com.reciver.common.enums.BusinessType;
import com.reciver.service.ReceiveDataService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lgb
 * @date 2020-8-25
 * @说明   数据接收
 **/
@RestController @Slf4j
@RequestMapping("/receive")
public class ReceiveDataController {
	
	@Resource
    private ReceiveDataService receiveDataService;
 
    @PostMapping(value="/data",produces="application/json;charset=UTF-8")
    @SysLog(title="获取中力设备数据",businessType=BusinessType.INSERT)
    public String getByZlJSON(@RequestBody JSONObject jsonParam) {
    	log.info("中力设备数据===="+JSONObject.toJSONString(jsonParam));
    	String code = jsonParam.getString("code");
    	if(code.equals("200")) {
    		JSONObject data=jsonParam.getJSONObject("data");
    		ZlBo zlBo=JSONObject.toJavaObject(data,ZlBo.class);
    		return receiveDataService.parseZl(zlBo);
    	}
        return "接收失败";
    }
	
    @PostMapping(value="/data_sbwy",produces="application/json;charset=UTF-8")
    @SysLog(title="获取中力设备数据Sbwy",businessType=BusinessType.INSERT)
    public String getByZlJSONSbwy(@RequestBody JSONObject jsonParam) {
    	log.info("中力设备数据Sbwy===="+JSONObject.toJSONString(jsonParam));
    	String code = jsonParam.getString("code");
    	try {
    		if(code.equals("200")) {
    			JSONObject data=jsonParam.getJSONObject("data");
    			ZlBoSbwy zlBoSbwy=JSONObject.toJavaObject(data,ZlBoSbwy.class);
    			receiveDataService.parseZlSbwy(zlBoSbwy);
    		}else {
    			return "数据格式错误!";
    		}
		} catch (Exception e) {
			e.printStackTrace();
			log.info(e.getMessage());
			return "error";
		}
		return "success";
    }
    
    @PostMapping(value="/jc_data",produces="application/json;charset=UTF-8")
    @SysLog(title="获取甲虫小型化设备数据",businessType=BusinessType.INSERT)
    public String getByJcJSON(@RequestBody JSONObject jsonParam) {
    	log.info("甲虫小型化设备数据===="+JSONObject.toJSONString(jsonParam));
    	String sn = jsonParam.getString("sn");
    	if(StringUtils.isEmpty(sn)) {
    		return "设备编号为空!";
    	}
    	JcBo jc=JSONObject.toJavaObject(jsonParam,JcBo.class);
        return receiveDataService.parseJc(jc);
    }
    
}